package com.gdth.bank.ui;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.InputType;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.bank.activity.CjkhjlActivity;
import com.gdth.bank.adapter.CjkhZcxxListViewAdapter;
import com.gdth.bank.adapter.CjkhZcxxListViewAdapter.OnDeleteClickListener;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.presenter.CjkhZcxxPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.CjkhZcxxView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.bank.widget.listener.TextWatcherListener;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjZcxxb;
import com.gdth.model.Item;
import com.gdth.utils.DateTimeUtil;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseToast;
import com.gdth.widget.dialog.DialogConfig;
import com.gdth.widget.dwonloadlist.BaseDownListTextView;

import org.greenrobot.eventbus.EventBus;

public class CjkhZcxxFragment extends BaseFrameLayout implements CjkhZcxxView{
	
	@ViewInject(id=R.id.cjkh_zcxx_zcsyz) BankDrawDownListTextView edit_zcsyz;
	@ViewInject(id=R.id.cjkh_zcxx_zjlx) BankDrawDownListTextView drawDownList_zjlx;
	@ViewInject(id=R.id.cjkh_zcxx_zjhm) BankEditText edit_zjhm;
	@ViewInject(id=R.id.cjkh_zcxx_zczl) BankDrawDownListTextView drawDownList_zczl;
	@ViewInject(id=R.id.cjkh_zcxx_zcmc) BankDrawDownListTextView edit_zcmc;
	@ViewInject(id=R.id.cjkh_zcxx_zcje) BankEditText edit_zcje;
	@ViewInject(id=R.id.cjkh_zcxx_input_zcmc) BankEditText cjkh_zcxx_input_zcmc;

	@ViewInject(id=R.id.cjkh_zcxx_bz) EditText edit_bz;
	@ViewInject(id=R.id.cjkh_zcxx_reset)
	TextView bankTextView_reset;
	@ViewInject(id=R.id.zcxx_save )
	Button zcxx_save;
	@ViewInject(id=R.id.zcxx_add )
	Button zcxx_add;
	@ViewInject(id=R.id.zcxx_change)
	Button zcxx_change;
	
	@ViewInject(id=R.id.root_layout) LinearLayout root_layout;
	@ViewInject(id=R.id.cjkh_zcxx_listview, itemClick="onItemClick") ListView listView_zcxx;
	
	List<Item> zjList, zczlList ,zcmclist;
	List<Item> jtcylist = new ArrayList<>();
	String jcuuid;
	
	private int create = 0, update = 1;
	
	private CjkhjlActivity activity;
	
	CjkhZcxxPresenter presenter;
	
	CjkhZcxxListViewAdapter adapter;
	
	List<TAppBfrjZcxxb> zcxxList;

	
	TAppBfrjZcxxb mTAppBfrjZcxxb;
	Long zhId;
	int id;
	String hquuid="";

	public CjkhZcxxFragment(Context context){
		super(context, R.layout.fragment_cjkhjl_zcxx);
		this.activity = (CjkhjlActivity) context;
	}
	
	public void initializeView(){
//		bankTextView_reset.setLabel("资产列表：");
//		bankTextView_reset.setImage(null);
		zjList = AppUtil.getCanshuByType(context, AppUtil.SHARE_DATA_ZJLX_CJKH);
		zczlList = AppUtil.getCanshuByType(context, AppUtil.SHARE_DATA_ZCZL);
		zcmclist = AppUtil.getCanshuByType(context,AppUtil.SHARE_DATA_ZCMC);
		edit_zcsyz.setLabel(R.string.cjkh_zcxx_zcsyz);
		edit_zcsyz.setOnSelectListener(new BaseDownListTextView.OnSelectListener() {
			@Override
			public void onchange(String key, String val) {
				edit_zjhm.setValue(key);
			}
		});
		//presenter.getjtcyxx(jcuuid);
//		drawDownList_zjlx.setLabel(R.string.cjkh_zcxx_zjlb);
//		drawDownList_zjlx.setItem(zjList);
//		drawDownList_zjlx.setValue("1");
		
		edit_zjhm.setLabel("身份证号：");
		edit_zjhm.setHint(R.string.cjkh_zcxx_zjhm_hint);
		edit_zjhm.setImage(null);
		
		drawDownList_zczl.setLabel(R.string.cjkh_zcxx_zczl);
		drawDownList_zczl.setItem(zczlList);
		drawDownList_zczl.setValue("1");
		
		edit_zcmc.setLabel("房屋类型：");
		edit_zcmc.setItem(zcmclist);
		edit_zcmc.setValue("1");
		edit_zcmc.setVisibility(View.GONE);

		cjkh_zcxx_input_zcmc.setLabel("资产名称：");
		cjkh_zcxx_input_zcmc.setHint("请输入资产名称");
		cjkh_zcxx_input_zcmc.setImage(null);

		drawDownList_zczl.setOnSelectListener(new BaseDownListTextView.OnSelectListener() {
			@Override
			public void onchange(String key, String val) {
				if(key.equals("2")){
					edit_zcmc.setVisibility(View.VISIBLE);
				}else {
					edit_zcmc.setVisibility(View.GONE);
                }
			}
		});

		edit_zcje.setLabel(R.string.cjkh_zcxx_zcje);
		edit_zcje.setHint(R.string.cjkh_zcxx_zcje_hint);
		edit_zcje.setImage(null);
		int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
		edit_zcje.setInputType(inputType);
		
		edit_bz.setHint(R.string.cjkh_zcxx_bz_hint);
		edit_bz.addTextChangedListener(new TextWatcherListener(context, edit_bz, 100));
		
		root_layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				// TODO Auto-generated method stub
				//比较Activity根布局与当前布局的大小
		        int heightDiff = root_layout.getRootView().getHeight() - root_layout.getHeight();
		        if(heightDiff > 600){
		        	//大小超过100时，一般为显示虚拟键盘事件
		        	listView_zcxx.setVisibility(View.GONE);
		        }else{
		        	//大小小于100时，为不显示虚拟键盘或虚拟键盘隐藏
		        	listView_zcxx.setVisibility(View.VISIBLE);
		       }
			}
			
		});

		zcxx_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				save();
			}
		});
		zcxx_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				presenter.getjtcyxx(jcuuid);
				reset();
			}
		});
		zcxx_change.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				//presenter.getjtcyxx(jcuuid);
				edit();
			}
		});
	}

	public void setchangejtcy(){
		presenter.getjtcyxx(jcuuid);
	}
	
	public void initializePresenter(){
		zcxxList = new ArrayList<TAppBfrjZcxxb>();
		presenter = new CjkhZcxxPresenter(this);
		adapter = new CjkhZcxxListViewAdapter(this.context, zcxxList);
		adapter.setItems(zczlList);
		adapter.setOnDeleteClickListener(new mOnDeleteClickListener());
		listView_zcxx.setAdapter(adapter);
	}
	
	DialogConfig DialogConfig_delete;
	private class mOnDeleteClickListener implements OnDeleteClickListener {
		@Override
		public void delete(int position) {
			// TODO Auto-generated method stub
			DialogConfig_delete = new DialogConfig(activity, "是否删除该记录", true, new DeleteClickListener(position));
			DialogConfig_delete.show();
		}
	};
	
	/**
	 * 删除资产信息
	 * @author arvin
	 *
	 */
	private class DeleteClickListener implements OnClickListener {
		private int position;
		
		public DeleteClickListener(int position) {
			this.position = position;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			TAppBfrjZcxxb tAppBfrjZcxxb = zcxxList.get(position);
			presenter.delete(tAppBfrjZcxxb);
			DialogConfig_delete.dismiss();
		}
	}
	
	/**
	 * 设置
	 * @param jcuuid
	 */
	public void setJcuuid(String jcuuid){
		this.jcuuid = jcuuid;
		presenter.getBfjlByUuid(jcuuid);
		presenter.getjtcyxx(jcuuid);
	}
	
	public void saveOrUpdate(int type){
		String zcsyz = edit_zcsyz.getValue();
		if (zcsyz.equals("")) {
			showMessage("资产所有者不能为空");
			return;
		}
//		Object zjlx_str = drawDownList_zjlx.getkey();
//		if (zjlx_str == null) {
//			showMessage("证件类型不能为空");
//			return;
//		}
//		int zjlx = Integer.valueOf(zjlx_str.toString());
		int zjlx = 1;
		String zjhm = edit_zjhm.getValue().toString();;
		if (zjlx == 1) {
			if (zjhm.equals("")) {
				showMessage("身份证号码不能为空");
				return;
			}
			String isTrue = "";
			try {
				isTrue = RegularUtil.IDCardValidate(zjhm);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!isTrue.equals("")) {
				showMessage(isTrue);
				return;
			}
		}
		Object zczl_str = drawDownList_zczl.getkey();
		if (zczl_str == null) {
			showMessage("资产种类不能为空");
			return;
		}
		int zczl = Integer.valueOf(zczl_str.toString());
		String zcfclxStr = "";
		if(edit_zcmc.getVisibility() == View.VISIBLE){
			zcfclxStr = edit_zcmc.getkey().toString();
			if (zcfclxStr.equals("")) {

				showMessage("房产类型不能为空");
				return;
			}
		}else{

		}
		String zcmcStr = cjkh_zcxx_input_zcmc.getValue();
//		int zcmc = Integer.valueOf(zcmcStr.toString());
		float zcje = 0;
		Object obj = edit_zcje.getValue();
		
		if (obj == null || obj.equals("")) {
			showMessage("资产金额不能为空");
			return;
		} else {
			zcje = Float.valueOf(obj.toString().replaceAll(",", ""));
		}
		if(id==0  &&  hquuid.equals("")){
			mTAppBfrjZcxxb = new TAppBfrjZcxxb();
			mTAppBfrjZcxxb.setCjsj(new Date());
			mTAppBfrjZcxxb.setBbh(1);
			mTAppBfrjZcxxb.setJcuuid(jcuuid);
			mTAppBfrjZcxxb.setSjzt(AppUtil.sjzt_create);
			mTAppBfrjZcxxb.setUuid(UUIDUtil.getUUID());
			mTAppBfrjZcxxb.setZcsyr(zcsyz);
			mTAppBfrjZcxxb.setZjlx(zjlx);
			mTAppBfrjZcxxb.setZcfclx(zcfclxStr);
			mTAppBfrjZcxxb.setZjhm(zjhm);
			mTAppBfrjZcxxb.setZczl(zczl);
			mTAppBfrjZcxxb.setZcmc(zcmcStr);
			mTAppBfrjZcxxb.setZcje(zcje);
			mTAppBfrjZcxxb.setBz(edit_bz.getText().toString());
			presenter.save(mTAppBfrjZcxxb, 0);
		}else if(id!=0 &&  hquuid !=null && mTAppBfrjZcxxb !=null){
			Date mDate = mTAppBfrjZcxxb.getCjsj();
			
//			if (DateTimeUtil.isToday(mDate)) {
				mTAppBfrjZcxxb.setBbh(mTAppBfrjZcxxb.getBbh() + 1);
				mTAppBfrjZcxxb.setJcuuid(jcuuid);
				mTAppBfrjZcxxb.setSjzt(AppUtil.sjzt_update);
				mTAppBfrjZcxxb.setUuid(hquuid);
				mTAppBfrjZcxxb.setZcsyr(zcsyz);
				mTAppBfrjZcxxb.setZjlx(zjlx);
				mTAppBfrjZcxxb.setZjhm(zjhm);
				mTAppBfrjZcxxb.setZczl(zczl);
				mTAppBfrjZcxxb.setZcfclx(zcfclxStr);
				mTAppBfrjZcxxb.setZcmc(zcmcStr);
				mTAppBfrjZcxxb.setZcje(zcje);
				mTAppBfrjZcxxb.setXgsj(new Date());
				mTAppBfrjZcxxb.setId(Long.parseLong(String.valueOf(id)));
				mTAppBfrjZcxxb.setBz(edit_bz.getText().toString());
				presenter.save(mTAppBfrjZcxxb, 1);
//			} else {
//				showMessage("只有当天新增记录才可以修改！");
//				return;
//			}
		}
		
	}
	
	/*
	 * 
	 * lzt
	 */
//	public void saveOrUpdate(int type) {
//		String zcsyz = edit_zcsyz.getValue().toString();
//		if (zcsyz.equals("")) {
//			showMessage("资产所有者不能为空");
//			return;
//		}
//		Object zjlx_str = drawDownList_zjlx.getkey();
//		if (zjlx_str == null) {
//			showMessage("证件类型不能为空");
//			return;
//		}
//		int zjlx = Integer.valueOf(zjlx_str.toString());
//		String zjhm = edit_zjhm.getValue().toString();;
//		if (zjlx == 1) {
//			if (zjhm.equals("")) {
//				showMessage("证件号码不能为空");
//				return;
//			}
//			String isTrue = "";
//			try {
//				isTrue = RegularUtil.IDCardValidate(zjhm);
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			if (!isTrue.equals("")) {
//				showMessage(isTrue);
//				return;
//			}
//		}
//		Object zczl_str = drawDownList_zczl.getkey();
//		if (zczl_str == null) {
//			showMessage("资产种类不能为空");
//			return;
//		}
//		int zczl = Integer.valueOf(zczl_str.toString());
//		
//		String zcmc = edit_zcmc.getValue().toString();
//		if (zcmc.equals("")) {
//			showMessage("资产名称不能为空");
//			return;
//		}
//		
//		float zcje = 0;
//		Object obj = edit_zcje.getValue();
//		
//		if (obj == null || obj.equals("")) {
//			showMessage("资产金额不能为空");
//			return;
//		} else {
//			zcje = Float.valueOf(obj.toString().replaceAll(",", ""));
//		}
//		
//		int save_zt = create;
//		if (type == 0) {
//			mTAppBfrjZcxxb = new TAppBfrjZcxxb();
//			mTAppBfrjZcxxb.setCjsj(new Date());
//			mTAppBfrjZcxxb.setBbh(1);
//		}
//		else {
//			if (mTAppBfrjZcxxb == null) {
//				showMessage("请先选择需要修改记录！");
//				return;
//			}
//			Date mDate = mTAppBfrjZcxxb.getCjsj();
//			
//			if (DateTimeUtil.isToday(mDate)) {
//				mTAppBfrjZcxxb.setBbh(mTAppBfrjZcxxb.getBbh() + 1);
//				save_zt = update;
//			} else {
//				showMessage("只有当天新增记录才可以修改！");
//				return;
//			}
//		}
//		
//		mTAppBfrjZcxxb.setJcuuid(jcuuid);
//		mTAppBfrjZcxxb.setSjzt(AppUtil.sjzt_create);
//		mTAppBfrjZcxxb.setUuid(UUIDUtil.getUUID());
//
//		mTAppBfrjZcxxb.setZcsyr(zcsyz);
//		mTAppBfrjZcxxb.setZjlx(zjlx);
//		mTAppBfrjZcxxb.setZjhm(zjhm);
//		mTAppBfrjZcxxb.setZczl(zczl);
//		mTAppBfrjZcxxb.setZcmc(zcmc);
//		mTAppBfrjZcxxb.setZcje(zcje);
//		mTAppBfrjZcxxb.setBz(edit_bz.getText().toString());
//		
//		presenter.save(mTAppBfrjZcxxb, save_zt);
//	}
	private long lastTime = 0;
	public void save() {
		if(System.currentTimeMillis() - lastTime  < 1000) {
			return ;
		}
		lastTime = System.currentTimeMillis();
		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
		saveOrUpdate(0);
	}
	
	public void edit() {

		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
//		saveOrUpdate(1);
		edit_zcsyz.mEditText.setEnabled(true);
		edit_zjhm.mEditText.setEnabled(false);
		drawDownList_zczl.mEditText.setEnabled(true);
		edit_zcmc.mEditText.setEnabled(true);
		cjkh_zcxx_input_zcmc.mEditText.setEnabled(true);
		edit_zcje.mEditText.setEnabled(true);
		edit_bz.setEnabled(true);
	}
	
	public void reset() {
		if(System.currentTimeMillis() - lastTime  < 1000) {
			return ;
		}
		lastTime = System.currentTimeMillis();
		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
		mTAppBfrjZcxxb = null;
		id = 0;
		hquuid = "";

		//drawDownList_zjlx.setValue("1");
		edit_zjhm.setValue("");
		drawDownList_zczl.setValue("1");
		if(null != jtcylist && jtcylist.size() >0)
			edit_zcsyz.setValue(jtcylist.get(0).getKey());

		edit_zcmc.setValue("1");
		edit_zcje.setValue("");
		cjkh_zcxx_input_zcmc.setValue("");
		edit_bz.setText("");
		edit_zcsyz.mEditText.setEnabled(true);
		edit_zjhm.mEditText.setEnabled(false);
		drawDownList_zczl.mEditText.setEnabled(true);
		edit_zcmc.mEditText.setEnabled(true);
		edit_zcmc.setVisibility(View.GONE);
		cjkh_zcxx_input_zcmc.setVisibility(View.VISIBLE);
		cjkh_zcxx_input_zcmc.mEditText.setEnabled(true);
		edit_zcje.mEditText.setEnabled(true);
		edit_bz.setEnabled(true);
	}
	
	public void onExitClick(){
		DialogConfig config = new DialogConfig(activity, "修改当前记录", true, exitClickListener);
		config.show();
	}
	
	
	private OnClickListener exitClickListener = new  OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		}
		
	};
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		mTAppBfrjZcxxb = zcxxList.get(arg2);
		//drawDownList_zjlx.setValue(mTAppBfrjZcxxb.getZjlx().toString());
		edit_zjhm.setValue(mTAppBfrjZcxxb.getZjhm());
		drawDownList_zczl.setValue(mTAppBfrjZcxxb.getZczl().toString());
		edit_zcsyz.setValue(mTAppBfrjZcxxb.getZcsyr().toString());
		for (int i = 0; i < jtcylist.size(); i++) {
			if(jtcylist.get(i).getValue().equals(mTAppBfrjZcxxb.getZcsyr().toString())){
				edit_zcsyz.setValue(jtcylist.get(i).getKey());
			}
		}

		if(mTAppBfrjZcxxb.getZczl().toString().equals("2")){
			edit_zcmc.setVisibility(View.VISIBLE);
		}else{
			edit_zcmc.setVisibility(View.GONE);
		}
		edit_zcmc.setValue(mTAppBfrjZcxxb.getZcfclx());
		cjkh_zcxx_input_zcmc.setValue(mTAppBfrjZcxxb.getZcmc());
		String je = new DecimalFormat("###,###,###.##").format(mTAppBfrjZcxxb.getZcje());
		edit_zcje.setValue(je);
		edit_bz.setText(mTAppBfrjZcxxb.getBz());
		zhId=mTAppBfrjZcxxb.getId();
		id = new Long(zhId).intValue();
		hquuid = mTAppBfrjZcxxb.getUuid();
		//drawDownList_zjlx.mEditText.setEnabled(false);
		edit_zjhm.mEditText.setEnabled(false);
		drawDownList_zczl.mEditText.setEnabled(false);
		edit_zcsyz.mEditText.setEnabled(false);
		edit_zcmc.mEditText.setEnabled(false);
		cjkh_zcxx_input_zcmc.mEditText.setEnabled(false);
		edit_zcje.mEditText.setEnabled(false);
		edit_bz.setEnabled(false);
	}
	
	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		activity.showProgressDialog(msg);
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		activity.hideProgressDialog();
	}
	
	@Override
	public void showMessage(String msg){
		activity.showMessage(msg);
	}

	@Override
	public void saveSuccess(Long id, int type) {
		reset();
		if (type == create)
			this.zcxxList.add(mTAppBfrjZcxxb);
		
		adapter.notifyDataSetChanged();
		showMessage("保存成功");
		reset();
		
	}

	@Override
	public void getAllBrjl(List<TAppBfrjZcxxb> arg0) {
		// TODO Auto-generated method stub
		for (TAppBfrjZcxxb tAppBfrjZcxxb : arg0) {
			this.zcxxList.add(tAppBfrjZcxxb);
		}
		
		adapter.notifyDataSetChanged();
	}

	@Override
	public void getAllJtcy(List<TAppBfrjJtcyb> arg0) {
		jtcylist.clear();
		for (TAppBfrjJtcyb tAppBfrjjccy : arg0) {
			Item item = new Item();
			item.setKey(tAppBfrjjccy.getZjhm());
			item.setValue(tAppBfrjjccy.getCyxm());
			jtcylist.add(item);
		}
		edit_zcsyz.setItem(jtcylist);
		if(null != jtcylist && jtcylist.size() >0)
			edit_zcsyz.setValue(jtcylist.get(0).getKey());

	}

	@Override
	public void deleteSuccess(TAppBfrjZcxxb mTAppBfrjZcxxb) {
		// TODO Auto-generated method stub
		this.zcxxList.remove(mTAppBfrjZcxxb);
		adapter.notifyDataSetChanged();
	}



}
